package com.herui.ota.service.iml;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.herui.ota.dao.mysql.AppPushMapper;
import com.herui.ota.entity.AppPush;
import com.herui.ota.entity.AppPushRecord;
import com.herui.ota.pojo.form.DeviceOtaConfigFilterForm;
import com.herui.ota.service.AppPushRecordService;
import com.herui.ota.service.AppPushService;
import com.mlnx.common.form.PageForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * author name: amanda.shan
 * create time: 2024-05-09 17:16:00
 */ 
@Service
public class AppPushServiceIml extends BaseService<AppPushMapper, AppPush> implements AppPushService{

	@Autowired
	private AppPushRecordService appPushRecordService;

	@Autowired
	@Override
	public void setBaseMapper(AppPushMapper baseMapper) {
		super.setBaseMapper(baseMapper);
	}

	@Override
	public IPage page(PageForm pageForm, Object filter) throws IllegalAccessException {

		Page page = baseMapper.listAppPushVoPage(new Page(pageForm.getCurrent(), pageForm.getSize()));
		return page;
	}

	@Override
	public List<AppPush> getNewAppPush(String pushUserKey, String language) {

//		AppPush appPush = getOne(new QueryWrapper<AppPush>().orderByDesc("create_time").last("limit 1"));
		List<AppPush> appPushs = list();
		List<AppPush> newAppPushs = new LinkedList<>();
		for (AppPush appPush: appPushs){
			if (appPush != null && language.equals(appPush.getLanguage())){
				if (!appPushRecordService.exist(pushUserKey, appPush.getId())){
					AppPushRecord appPushRecord = new AppPushRecord();
					appPushRecord.setAppPushId(appPush.getId());
					appPushRecord.setPushUserKey(pushUserKey);
					appPushRecordService.save(appPushRecord);

					newAppPushs.add(appPush);
				}
			}
		}
		return newAppPushs;
	}
}

